package com.ruoyi.douyin.service.open.imp;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.constant.ConstantDouYinBase;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.vo.AuthVo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.douyin.service.open.UserOpenDataService;
import com.ruoyi.douyin.utils.HttpUtils;
import com.ruoyi.douyin.vo.UserOpenDataVideoDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserOpenDataServiceImp implements UserOpenDataService {
    @Autowired
    private RedisCache redisCache;

    /**
     * 获取用户视频情况
     */
    @Override
    public AjaxResult videoData(UserOpenDataVideoDataVo userOpenDataVideoDataVo, String type) {
        AuthVo authVo = redisCache.getCacheObject(userOpenDataVideoDataVo.getOpenId());
        if (authVo == null) return AjaxResult.error("抖音授权过期");
        String url = null;
        switch (type) {
            case "video":
                // 获取用户视频情况
                url = ConstantDouYinBase.Url.BASE_URL.getUrl() + ConstantDouYinBase.Url.DATA_OPEN_USER_VIDEO_STATUE.getUrl();
                break;
            case "fans":
                // 获取用户粉丝数
                url = ConstantDouYinBase.Url.BASE_URL.getUrl() + ConstantDouYinBase.Url.DATA_OPEN_USER_FANS_STATUE.getUrl();
                break;
            case "like":
                // 获取用户点赞数
                url = ConstantDouYinBase.Url.BASE_URL.getUrl() + ConstantDouYinBase.Url.DATA_OPEN_USER_LIKE_STATUE.getUrl();
                break;
            case "comment":
                // 获取用户评论数
                url = ConstantDouYinBase.Url.BASE_URL.getUrl() + ConstantDouYinBase.Url.DATA_OPEN_USER_COMMENT_STATUE.getUrl();
                break;
            case "share":
                // 获取用户分享数
                url = ConstantDouYinBase.Url.BASE_URL.getUrl() + ConstantDouYinBase.Url.DATA_OPEN_USER_SHARE_STATUE.getUrl();
                break;
            case "index":
                // 获取用户主页访问数
                url = ConstantDouYinBase.Url.BASE_URL.getUrl() + ConstantDouYinBase.Url.DATA_OPEN_USER_MINE_INDEX_STATUE.getUrl();
                break;
        }
        HashMap<String, Object> param = new HashMap<>();
        param.put("open_id", userOpenDataVideoDataVo.getOpenId());
        param.put("access_token", authVo.getAccessToken());
        param.put("date_type", userOpenDataVideoDataVo.getDataType());
        String paramAdd = HttpUtils.paramAdd(url, param);
        try {
            String result = HttpUtil.get(paramAdd);
            return AjaxResult.success("success", JSON.parse(result));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    @Override
    @Cacheable(value = "douyin:open:user:data", key = "#p0.openId")
    public AjaxResult getAll(UserOpenDataVideoDataVo userOpenDataVideoDataVo) {
        Object video = this.videoData(userOpenDataVideoDataVo, "video").get("data");
        Object fans = this.videoData(userOpenDataVideoDataVo, "fans").get("data");
        Object like = this.videoData(userOpenDataVideoDataVo, "like").get("data");
        Object comment = this.videoData(userOpenDataVideoDataVo, "comment").get("data");
        Object share = this.videoData(userOpenDataVideoDataVo, "share").get("data");
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("video", video);
        resultMap.put("fans", fans);
        resultMap.put("like", like);
        resultMap.put("comment", comment);
        resultMap.put("share", share);
        return AjaxResult.success(resultMap);
    }
}
